//
//  LHPhotoBrowerViewController.swift
//  sinaWeibo
//
//  Created by LiHong on 15/11/8.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

class LHPhotoBrowerViewController: UIViewController {

    //MARK: - 属性
    private let reuseIdentifier = "cellIdentifier"
    
    //流水布局
    private var layout = UICollectionViewFlowLayout()
    
    //大图的urls
    private var urls: [NSURL]
    
    private var selectedIndex: Int
    //MARK: - 构造函数
    init(urls: [NSURL], selectedIndex: Int){
        self.urls = urls
        self.selectedIndex = selectedIndex
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        // Do any additional setup after loading the view.
    }
    
    //MARK: - 按钮点击事件
    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func save() {
        
    }
    
    private func prepareUI() {
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        view.addSubview(pageLabel)
        
        closeButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        saveButton.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        saveButton.translatesAutoresizingMaskIntoConstraints = false
        pageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        let views = [
            "cv" : collectionView,
            "cb" : closeButton,
            "sb" : saveButton,
            "pl" : pageLabel
        ]
        //collectionView
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        //页码
        view.addConstraint(NSLayoutConstraint(item: pageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: pageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 20))
        
        //关闭，保存按钮
        //高度35，距离底部8
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[cb(35)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sb(35)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        //水平
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[cb(75)]-(>=0)-[sb(75)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        prepareCollectionView()
    }

    //MARK: - 准备collectionView
    private func prepareCollectionView() {
        collectionView.registerClass(LHPhotoBrowserCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        layout.itemSize = view.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView.bounces = false
        collectionView.pagingEnabled = true
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
    }
    
    //MARK: - 懒加载
    //collectionView
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.layout)
    
    //关闭
    private lazy var closeButton: UIButton = UIButton(bkgImage: "health_button_orange_line", title: "关闭", titleColor: UIColor.whiteColor(), fontSize: 12)
    
    //保存
    private lazy var saveButton: UIButton = UIButton(bkgImage: "health_button_orange_line", title: "保存", titleColor: UIColor.whiteColor(), fontSize: 12)
    
    //页码的label
    private lazy var pageLabel = UILabel(fontSize: 15, textColor: UIColor.whiteColor())
    
}

extension LHPhotoBrowerViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LHPhotoBrowserCell
        cell.backgroundColor = UIColor.blackColor()
        cell.url = urls[indexPath.item]
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let indexPath = collectionView.indexPathsForVisibleItems().first!
        
        
    }
}
